package com.shrct.businessprocess.project.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.project.RCCommissionYearConfig;
import com.shrct.businessprocess.project.RCSalesAssignment;
import com.shrct.businessprocess.project.RCSalesCommissionYearConfig;
import com.shrct.businessprocess.project.RCSalesCommissionYearSettlement;
import com.shrct.businessprocess.project.RCSalesTeam;
import com.shrct.businessprocess.project.RCTeamMemberAssignment;
import com.shrct.businessprocess.project.impl.RCSalesAssignmentImpl;
import com.shrct.businessprocess.project.impl.RCSalesTeamImpl;
import com.shrct.businessprocess.project.proxy.RCTeamMemberAssignmentProxy;
import com.uniquesoft.uidl.validation.Validates;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCTEAMMEMBERASSIGNMENT_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcteammemberassignment")
public class RCTeamMemberAssignmentImpl extends BaseModelObject implements RCTeamMemberAssignment {
  public RCTeamMemberAssignmentImpl() {
    super();
  }
  
  public RCTeamMemberAssignmentImpl(final RCSalesTeam team) {
    super();
    this.team = team;
  }
  
  @JoinColumn(name = "team_id")
  @ManyToOne(targetEntity = RCSalesTeamImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCSalesTeam team;
  
  public RCSalesTeam getTeam() {
    return this.team;
  }
  
  public RCTeamMemberAssignment setTeam(final RCSalesTeam team) {
    this.team = team;
    return this;			
    
  }
  
  @JoinColumn(name = "sales_id")
  @ManyToOne(targetEntity = RCSalesAssignmentImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCSalesAssignment sales;
  
  public RCSalesAssignment getSales() {
    return this.sales;
  }
  
  public RCTeamMemberAssignment setSales(final RCSalesAssignment sales) {
    this.sales = sales;
    return this;			
    
  }
  
  private RCSalesCommissionYearConfig getConfig() {
    RCSalesCommissionYearConfig _config = this.team.getConfig();
    RCCommissionYearConfig _overallConfig = _config.getOverallConfig();
    IGenericQuery<RCSalesCommissionYearConfig> query=getObjectFactory().createGenericQuery(RCSalesCommissionYearConfig.class,"select e from com.shrct.businessprocess.project.impl.RCSalesCommissionYearConfigImpl as e   where (((e.overallConfig) = (:_overallConfig)) and ((e.saler) = (:sales))) and  e.active = 1      ").setParameter("_overallConfig",_overallConfig).setParameter("sales",sales);
    RCSalesCommissionYearConfig _uniqueResult = query.uniqueResult();
    return _uniqueResult;
  }
  
  public RCSalesCommissionYearSettlement getLastSettlement() {
    RCSalesCommissionYearConfig _config = this.getConfig();
    RCSalesCommissionYearSettlement _settlement = _config==null?(RCSalesCommissionYearSettlement)null:_config.getSettlement();
    return _settlement;
  }
  
  public Double getTeamGrossProfitGoal() {
    RCSalesCommissionYearConfig _config = this.getConfig();
    Double _baseGrossProfitGoal = _config.getBaseGrossProfitGoal();
    return _baseGrossProfitGoal;
  }
  
  public Double getContractProfit() {
    RCSalesCommissionYearSettlement _lastSettlement = this.getLastSettlement();
    Double _contractProfit = _lastSettlement==null?(Double)null:_lastSettlement.getContractProfit();
    Double _nullsafe = NumberExtensions.nullsafe(_contractProfit);
    return _nullsafe;
  }
  
  public Double getTeamContributeAmount() {
    double _xblockexpression = (double) 0;
    {
      RCSalesCommissionYearSettlement _lastSettlement = this.getLastSettlement();
      boolean _equals = Objects.equal(_lastSettlement, null);
      if (_equals) {
        return Double.valueOf(0d);
      }
      final RCSalesCommissionYearSettlement ls = this.getLastSettlement();
      Double _cggp = ls.getCggp();
      Double _totalCost = ls.getTotalCost();
      double _minus = DoubleExtensions.operator_minus(_cggp, _totalCost);
      Double _commissionAmountTarget = ls.getCommissionAmountTarget();
      double _minus_1 = (_minus - (_commissionAmountTarget).doubleValue());
      Double _overflowCommissionAmountTarget = ls.getOverflowCommissionAmountTarget();
      final double result = (_minus_1 - (_overflowCommissionAmountTarget).doubleValue());
      _xblockexpression = (result);
    }
    return Double.valueOf(_xblockexpression);
  }
  
  public Double getTeamContributeAmountTarget() {
    double _xblockexpression = (double) 0;
    {
      RCSalesCommissionYearSettlement _lastSettlement = this.getLastSettlement();
      boolean _equals = Objects.equal(_lastSettlement, null);
      if (_equals) {
        return Double.valueOf(0d);
      }
      final RCSalesCommissionYearSettlement ls = this.getLastSettlement();
      Double _contractProfit = ls.getContractProfit();
      Double _totalCost = ls.getTotalCost();
      double _minus = DoubleExtensions.operator_minus(_contractProfit, _totalCost);
      Double _commissionAmountTarget = ls.getCommissionAmountTarget();
      double _minus_1 = (_minus - (_commissionAmountTarget).doubleValue());
      Double _overflowCommissionAmountTarget = ls.getOverflowCommissionAmountTarget();
      final double result = (_minus_1 - (_overflowCommissionAmountTarget).doubleValue());
      _xblockexpression = (result);
    }
    return Double.valueOf(_xblockexpression);
  }
  
  public void checkValid() {
    RCSalesCommissionYearConfig _config = this.getConfig();
    boolean _notEquals = (!Objects.equal(_config, null));
    Validates.Assert(_notEquals, "\u6210\u5458\u5FC5\u987B\u8BBE\u7F6E\u672C\u5E74\u5EA6\u9500\u552E\u6307\u6807");
  }
  
  public static RCTeamMemberAssignment create(final RCSalesTeam team, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.project.RCTeamMemberAssignment rCTeamMemberAssignment = new com.shrct.businessprocess.project.impl.RCTeamMemberAssignmentImpl(
    	team
    );
    objectFactory.create(rCTeamMemberAssignment);
    return rCTeamMemberAssignment;			
    
  }
  
  public static RCTeamMemberAssignment createRCTeamMemberAssignmentImpl(final RCSalesTeam team, final IObjectFactory objectFactory, final Procedure1<RCTeamMemberAssignment> init) {
    com.shrct.businessprocess.project.RCTeamMemberAssignment rCTeamMemberAssignment = new com.shrct.businessprocess.project.impl.RCTeamMemberAssignmentImpl(
    	team
    );
    objectFactory.create(rCTeamMemberAssignment);
    init.apply(rCTeamMemberAssignment);
    
    return rCTeamMemberAssignment;			
    
  }
  
  public RCTeamMemberAssignmentProxy toProxy() {
    com.shrct.businessprocess.project.proxy.RCTeamMemberAssignmentProxy proxy = new com.shrct.businessprocess.project.proxy.RCTeamMemberAssignmentProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
